<?php
/*
 * Attributes:
 * action:  Action to send the form to
 * module:  Module to search for the action (optional)
 * anquor:  Anquor to jump to (optional)
 * target:  Target frame to jump to (optional)
 * module:  Module to search for the action (optional)
 * name:    Name of the form (optional; required, if validation is used)
 * method:  Method to use for the form (optional; default value 'POST')
 * enctype: Encoding type to use for sending form data
 * 
 */

// TODO add target attribute, if validate name is required
class h_formHandler
{
    
    var $name = null;

    function startElement (& $sender, $attrs)
    {
        $attrs['type'] = 'form';
        $sender->pushParent($attrs);
        $run = &phpinchains_RuntimeConfig::getInstance();
        $actionParam   = $run->get("actionParam");
        $actionParamId = $run->get("actionParamId");
        $moduleParam   = $run->get("moduleParam");
        $moduleParamId = $run->get("moduleParamId");
        
        $tag = '$out->write("<form';
        if (empty($attrs['value']) && empty($attrs['action']))
        {
            die ("Attribute value or action is missing in form tag!\n");
        }
        
        if (!empty($attrs['value']))
            $tag .= ' action=\\"".'.$attrs['value'].(isset($attrs['anquor'])?'#'.$attrs['anquor']:'').'."\\"';
        else
        {
            if (!empty($attrs['action']) && $actionParam == 'pathInfo')
            {
                if (!empty($attrs['module']))
                    $tag .= ' action=\\"".(defined(\'MODEL2_SCRIPT_NAME\')?MODEL2_SCRIPT_NAME:$env->getServer("SCRIPT_NAME"))."/".'.$attrs['module'].'."/".'.$attrs['action'].(isset($attrs['anquor'])?'.\'#\'.'.$attrs['anquor']:'').'."\\"';
                elseif ($moduleParam)
                    $tag .= ' action=\\"".(defined(\'MODEL2_SCRIPT_NAME\')?MODEL2_SCRIPT_NAME:$env->getServer("SCRIPT_NAME"))."/'.$moduleParam.'/".'.$attrs['action'].(isset($attrs['anquor'])?'.\'#\'.'.$attrs['anquor']:'').'."\\"';
                else
                    $tag .= ' action=\\"".(defined(\'MODEL2_SCRIPT_NAME\')?MODEL2_SCRIPT_NAME:$env->getServer("SCRIPT_NAME"))."/".'.$attrs['action'].(isset($attrs['anquor'])?'.\'#\'.'.$attrs['anquor']:'').'."\\"';
            }
            else
                $tag .= ' action=\\"?'.(isset($attrs['anquor'])?'#".'.$attrs['anquor'].'."':'').'\\"';
        }
        if (!empty($attrs['enctype']))
        {
            $tag .= ' enctype=\\"".'.$attrs['enctype'].'."\\"';
        }
        if (!empty($attrs['name']))
        {
            $tag .= ' name=\\"".'.$attrs['name'].'."\\"';
        }
        foreach ($attrs as $n => $v) {
            if (!in_array($n, array('value', 'type', 'action', 'module', 'name', 'method', 'enctype', 'validate',
               'validationParam', 'validationErrorStyle', 'validationErrorClass', 'pspc')))
            $tag .= ' '.$n.'=\\"".'.$v.'."\\"';
        }
        $tag .= ' method=\\"".'.(isset($attrs['method'])?$attrs['method']:"'POST'").'."\\">");'."\n";
        $tag .= 'if (!empty($GLOBALS[\'doc_id\'])) $out->write("<input type=\\"hidden\\" name=\\"doc_id\\" value=\\"${GLOBALS[\'doc_id\']}\\"/>");';
        if ($attrs['action'] && $actionParam == 'queryString')
        {
            if ($attrs['module'])
                $tag .= '$out->write("<input type=\\"hidden\\" name=\\"'.$moduleParamId.'\\" value=\\"".'.$attrs['module'].'."\\"/>");';            
            elseif ($moduleParam)
                $tag .= '$out->write("<input type=\\"hidden\\" name=\\"'.$moduleParamId.'\\" value=\\""."'.$moduleParam.'"."\\"/>");';            
            $tag .= '$out->write("<input type=\\"hidden\\" name=\\"'.$actionParamId.'\\" value=\\"".'.$attrs['action'].'."\\"/>");';
        }
        $env = & env::getInstance();
        if ($lastView = $env->getAttribute('__lastView'))
        {
            $tag .= '$out->write("<input type=\\"hidden\\" name=\\"__lastView\\" value=\\"'.$lastView.'\\"/>");';
        }
        if (!empty($attrs['name']))
        {
            $tag .= '$out->write("<input type=\\"hidden\\" name=\\"__formName\\" value=\\"".'.$attrs['name'].'."\\"/>");';
        }
       	$tag .= "\$run = &phpinchains_RuntimeConfig::getInstance();\n";
       	$tag .= "if (\$run->get('sec_code_enabled')) {\n";
        $tag .= '    $out->write("<input type=\\"hidden\\" name=\\"__ss\\" value=\\"".$env->getSession(\'phpinchains_sec_code\')."\\"/>");';
       	$tag .= "}\n";
       	$tag .= "if (is_array(\$run->get('additional_parameters'))) {\n";
       	$tag .= '    $'."__params = \$run->get('additional_parameters');\n";
       	$tag .= "    foreach (\$__params as \$n => \$v) {\n";
        $tag .= '        $out->write("<input type=\\"hidden\\" name=\\"$n\\" value=\\"$v\\"/>");';
       	$tag .= "    }\n";
       	$tag .= "}\n";
       	$sender->appendCode($tag);
        // validation code
        if (!empty($attrs['name']))// && $attrs['validate'])
        {
            $code  = "      if (\$formname == ".$attrs['name'].")\n        {\n";
            if (!empty($attrs['validate']))
                $code .= "\n        \$this->validateField(\$env, \$actionform, ".$attrs['name'].", ".(isset($attrs['validationParam'])?$attrs['validationParam']:"''").', '.$attrs['validate'].");\n";
            $this->name = $attrs['name'];
            $sender->appendValidationCode($code);
        }
    }

    function endElement (& $sender)
    {
        $sender->popParent();
        $sender->appendCode('$out->write("</form>");'."\n");
        // validation code
        if ($this->name != null)
        {
            $code = "\n     \$returnVal = (\$returnVal && !\$validationErrors->hasErrors());\n      }\n";
            $sender->appendValidationCode($code);
            $this->name = null;
        }
    }

}

?>